package com.example.chengenglong;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;

import java.util.ArrayList;


@SuppressWarnings("ALL")
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
    private final ArrayList<WeatherInfo> arrayList;
    private  OnItemClickListener onItemClickListener = null;

    public MainAdapter(ArrayList<WeatherInfo> arrayList) {
        this.arrayList = arrayList;
    }



    @NonNull
    @Override
    public MainAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MainAdapter.ViewHolder holder, int position) {
        WeatherInfo info = arrayList.get(position);
        holder.wendu.setText(info.getTem2()+"~" + info.getTem1()+"," + info.getWea());
        holder.riqi.setText(info.getDate());

        holder.wendu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null ) onItemClickListener.onQuery(position,info);
            }
        });

        holder.wendu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null ) onItemClickListener.onClick(position,info);
            }
        });

    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    @SuppressWarnings("unused")
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;

    }

    @SuppressWarnings("unused")
    public interface OnItemClickListener {

        void onQuery(int position, WeatherInfo info);
        void onClick(int position, WeatherInfo info);

    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        private final AppCompatTextView wendu;
        private final AppCompatTextView riqi;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            wendu = itemView.findViewById(R.id.wendu);
            riqi = itemView.findViewById(R.id.riqi);
        }
    }
}
